<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_anchor\Entity\ShusyAnchor;
use Drupal\ssy_live_room\Entity\SsyLiveRoom;

/**
 * @OA\Schema(
 *   schema="Anchor",
 *   type="object",
 *   description="Anchor"
 * )
 */
class Anchor extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_anchor';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyAnchor) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="name",
         *   type="string",
         *   description="名称",
         * ),
         */
        'name' => (string) $this->entity->name,
        /**
         * @OA\Property(
         *   property="avatar",
         *   type="string",
         *   description="头像",
         * ),
         */
        'avatar' => $this->imageStyleUrl('AnchorAvatar', $this->wrapper->field_image->value()),
        /**
         * @OA\Property(
         *   property="title",
         *   type="string",
         *   description="头衔",
         * ),
         */
        'title' => (string) $this->entity->title,
        /**
         * @OA\Property(
         *   property="summary",
         *   type="string",
         *   description="摘要",
         * ),
         */
        'summary' => (string) $this->entity->summary,
        /**
         * @OA\Property(
         *   property="url",
         *   type="string",
         *   description="直播间URL参数",
         * ),
         */
        'url' => $this->getLiveUrl($this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getLiveUrl(ShusyAnchor $anchor) {
    $room = ssy_live_room_load_single(FALSE, ['anchor_id' => $anchor->id]);
    if ($room instanceof SsyLiveRoom) {
      $custom_params = drupal_json_encode([
        'uid' => $anchor->uid,
      ]);
      return "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?open_share_ticket=1&room_id={$room->room_id}&custom_params={$custom_params}";
    }
    return FALSE;
  }
}
